package models

import (
	"eat-server/global"
	"gorm.io/gorm"
	"time"
)

type Goods struct {
	Id          uint64         `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);default:NULL;" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);default:NULL;" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
	GoodsId     int32          `gorm:"column:goods_id;type:int(20);comment:商品id;not null;" json:"goods_id"`              // 商品id
	GoodsPrice  float64        `gorm:"column:goods_price;type:decimal(10, 2);comment:商品价格;not null;" json:"goods_price"` // 商品价格
	GoodsMs     string         `gorm:"column:goods_ms;type:varchar(100);comment:商品描述;not null;" json:"goods_ms"`         // 商品描述
	GoodsCover  string         `gorm:"column:goods_cover;type:varchar(100);comment:商品封面;not null;" json:"goods_cover"`   // 商品封面
	GoodsStatus string         `gorm:"column:goods_status;type:varchar(100);comment:商品状态;not null;" json:"goods_status"` // 商品状态
	GoodsLike   int64          `gorm:"column:goods_like;type:bigint(3);comment:商品喜欢;default:NULL;" json:"goods_like"`    // 商品喜欢
}

type Orders struct {
	Id        uint64         `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);default:NULL;" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);default:NULL;" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
	UserId    int64          `gorm:"column:user_id;type:bigint(20);comment:用户id;not null;" json:"user_id"`          // 用户id
	OrderSn   string         `gorm:"column:order_sn;type:char(36);comment:订单号;not null;" json:"order_sn"`           // 订单号
	PayType   int8           `gorm:"column:pay_type;type:tinyint(4);comment:支付类型:1:支付宝;default:1;" json:"pay_type"` // 支付类型:1:支付宝
	Status    int8           `gorm:"column:status;type:tinyint(1);comment:订单状态:1:待支付;default:1;" json:"status"`     // 订单状态:1:待支付
	PayTime   int64          `gorm:"column:pay_time;type:bigint(20);comment:支付时间;default:NULL;" json:"pay_time"`    // 支付时间
	Amount    float64        `gorm:"column:amount;type:decimal(10, 2);comment:订单金额;default:0.00;" json:"amount"`    // 订单金额
}

type OrderGoods struct {
	Id         uint64         `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);default:NULL;" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);default:NULL;" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
	OrderId    int64          `gorm:"column:order_id;type:bigint(20);comment:订单id;not null;" json:"order_id"`         // 订单id
	GoodsId    int64          `gorm:"column:goods_id;type:bigint(20);comment:商品id;not null;" json:"goods_id"`         // 商品id
	Num        int64          `gorm:"column:num;type:bigint(20);comment:商品数量;not null;" json:"num"`                   // 商品数量
	Price      float64        `gorm:"column:price;type:decimal(10, 2);comment:商品单价;not null;" json:"price"`           // 商品单价
	GoodsTitle string         `gorm:"column:goods_title;type:varchar(100);comment:商品标题;not null;" json:"goods_title"` // 商品标题
	GoodsCover string         `gorm:"column:goods_cover;type:varchar(200);comment:商品封面;not null;" json:"goods_cover"` // 商品封面
}

func (g *Goods) Info(goodsId int) error {
	return global.DB.Where("id = ?", goodsId).Find(&g).Error
}

func (o *Orders) AddOrder() error {
	return global.DB.Create(&o).Error
}
func (o *OrderGoods) AddOrderGoods() error {
	return global.DB.Create(&o).Error
}
